home *** CD-ROM | disk | FTP | other *** search
/ Gold Medal Software 3 / Gold Medal Software - Volume 3 (Gold Medal) (1994).iso / prog / word.arj / PLATFORM.C < prev    next >
C/C++ Source or Header  |  1993-01-28  |  8KB  |  318 lines

  1. #include <dos.h>
  2. #include <alloc.h>
  3. #include <stdio.h>
  4. #include <conio.h>
  5. #include <wgt.h>
  6. #include <scroll.h>
  7. #include <time.h>
  8.  
  9. // Feel free to modify any of this code!
  10. // Generic platform game using a character which is 32 pixels high.
  11. // Modify the defines and character stats below for a quick and instant
  12. // platform game.  This should really help you get started with a game
  13. // of this type.
  14.  
  15. // The character is a little stiff, and there's nothing else on the map,
  16. // but hey! It is only a demo!
  17.  
  18.  
  19. #define YOU 50
  20. #define WALKRIGHT1 1
  21. #define WALKRIGHT2 8
  22. #define STANDRIGHT 9
  23. #define JUMPRIGHT 10
  24. #define WALKLEFT1 11
  25. #define WALKLEFT2 18
  26. #define STANDLEFT 19
  27. #define JUMPLEFT 20
  28.  
  29. #define LEFT 1
  30. #define RIGHT 2
  31.  
  32. #define SOLID 1
  33. #define HOLLOW 0
  34.  
  35. void checkfeet(void);
  36. void checkhead(void);
  37. void checkright(void);
  38. void checkleft(void);
  39.  
  40. int oldx,oldy,direction,anim;
  41. int jumping,gravity;
  42. int spx,spy;
  43.  
  44. int charheight=32;    // main character's height in pixels
  45. int extra=2;        // leave a bit of extra space above head and below
  46.             // feet to fit through tight places in the map.
  47.  
  48. // 4 TIME structures to hold different info
  49. time_t tim1,tim2,tim3,tim4;
  50. // The number of times the screen is updated
  51. long updates;
  52.  
  53. int windx,windy;    // window vars
  54.  
  55. wgtmap gamemap;            // our world map
  56.  
  57. int kbdon[100]={0,0,0,0,0,0};    // our keyboard on/off array;
  58. int kbdscanlist[100]={72,80,75,77,1,29};    // our keyboard scan codes;
  59. // You must have the above two if you want the keyboard interrupt 
  60.  
  61. color palette[256];        // our palette of colours
  62.  
  63. block blocks[1001];        // our blocks for the map
  64. block sprites[1001];        // our sprites 
  65.  
  66. int i;
  67.  
  68. void main(void)
  69. {
  70. printf("Wordup Graphics Toolkit     Platform SCROLLING DEMO\n");
  71. printf("Arrow keys move, CTRL jumps. Up/down looks in direction\n");
  72. printf("\nWindow Width (2-17):");
  73. scanf("%i",&windx);
  74. printf("\nWindow Height (2-10):");
  75. scanf("%i",&windy);
  76.  
  77. vga256();                    // init
  78. wloadsprites(&palette,"tiles.spr",blocks);        // load tiles
  79. wloadsprites(&palette,"object.spr",sprites);        // load objects
  80.  
  81. gamemap=wloadmap("map.wmp");        // load our world map
  82.  
  83. winitscroll(windx,windy);    
  84.  
  85. wnormscreen();                    // go back to normal screen
  86. wcls(0);                    // clear it
  87.  
  88. wshowwindow(0,10,gamemap);            // start looking at world
  89.                         // at 0,0
  90. installkbd();                    // start new keyboard interrupt
  91. numsprites=100;
  92. // original sprite locations and numbers are stored within the map file
  93. // created by the map maker...
  94.  
  95.  
  96. jumping=0; gravity=0;
  97. anim=1;
  98.  
  99. gettime(&tim1);
  100. // get beginning time
  101. window(1,1,80,25);
  102.  
  103. do
  104. {
  105. gettime(&tim3);
  106. // used to store time needed for one frame
  107.  
  108. spx=0;
  109. spy=0;
  110. oldx=wobject[YOU].x;
  111. oldy=wobject[YOU].y;
  112.  
  113. if (jumping==1)
  114.   gravity+=2;
  115. if (gravity>15)
  116.    gravity=15;
  117.  
  118. if ((kbdon[5]==1) & (jumping==0))
  119.    {
  120.    jumping=1;
  121.    gravity=-14;        // make smaller for a higher jump
  122.    }
  123.  
  124. if (kbdon[2]==1)        // Pressing left
  125.   {
  126.   wobject[YOU].x-=8;
  127.   checkleft();
  128.   if (direction !=LEFT)
  129.     {
  130.     direction=LEFT;
  131.     anim=WALKLEFT1;
  132.     }
  133.   anim++;
  134.   if (anim>WALKLEFT2)
  135.     anim=WALKLEFT1;
  136.   }
  137. else if (kbdon[3]==1)    // Pressing right
  138.   {
  139.   wobject[YOU].x+=8;
  140.   checkright();
  141.   if (direction !=RIGHT)
  142.     {
  143.     direction=RIGHT;
  144.     anim=WALKRIGHT1;
  145.     }
  146.   anim++;
  147.   if (anim>WALKRIGHT2)
  148.     anim=WALKRIGHT1;
  149.   }
  150.  
  151. wobject[YOU].num=anim;
  152.  
  153. if (wobject[YOU].x==oldx)    // haven't moved left or right?
  154.  
  155.   if (direction==LEFT)
  156.      wobject[YOU].num=STANDLEFT;
  157.   else wobject[YOU].num=STANDRIGHT;
  158.  
  159. wobject[YOU].y+=gravity;
  160. if (wobject[YOU].y<0) wobject[YOU].y=0;
  161. if (gravity<0)
  162.    checkhead();
  163.  
  164.  
  165. if (jumping==1)
  166.   if (direction==LEFT)
  167.      wobject[YOU].num=JUMPLEFT;
  168.   else wobject[YOU].num=JUMPRIGHT;
  169.  
  170.  checkfeet();
  171.  
  172. spx=wobject[YOU].x-worldx-windowmaxx/2;
  173. spy=wobject[YOU].y-worldy-windowmaxy/2;
  174.  
  175. if (kbdon[0]==1)        // Pressing up
  176.   {
  177.      spy=-4;
  178.   }
  179. if (kbdon[1]==1)    // Pressing down
  180.   {
  181.  spy+=4;
  182.  }
  183.  
  184. //moveguys();
  185. wscrollwindow(spx,spy,gamemap);    // update the scrolling window
  186. wshowobjects();
  187.  
  188. wcopyscroll(30,10);
  189. nosound();
  190. updates++;
  191. // Run once, to find out the frame rate. Now take the milliseconds
  192. // per frame value, and put it in the while statement. Unremark this
  193. // while statement, and give it a try using a smaller scrolling window.
  194. // It should run at the same speed regardless of the window size.
  195. // Of course if it is a larger window, it will be slower than you want it.
  196. /*do {
  197.    gettime(&tim4);
  198.    } while (wtimer(tim3,tim4)<5);*/ 
  199. // Each frame takes at least 5 milliseconds right now. Raise and lower
  200. // the number to change the speed of the game.
  201.  
  202. } while (kbdon[4] !=1);            // until right button is pressed
  203. gettime(&tim2);
  204. // get final time
  205.  
  206. uninstallkbd();
  207.  
  208. wendscroll();
  209. wfreesprites(blocks);
  210. wfreesprites(sprites);
  211. wfreemap(gamemap);
  212. textmode(C80);
  213. printf("\n# seconds: %d",wtimer(tim1,tim2)/100);
  214. printf("\n# ms: %i",wtimer(tim1,tim2));
  215. printf("\n# updates: %i",updates);
  216.  
  217.  
  218. printf("\nAverage frame rate: %2.2f frames/sec",(float)updates/(float)(wtimer(tim1,tim2)/100));
  219. printf("\nMilliseconds per frame: %2.2f ms/f",wtimer(tim1,tim2)/(float)updates);
  220. printf("\nRemember this-------------^");
  221. // now that you now this, you can delete all uses of tim1, and tim2
  222. // and only use tim3,tim4 to control timing. Try it!
  223.  
  224. getch();
  225. }
  226.  
  227.  
  228. void checkright(void)
  229. {
  230. int j,k,l;
  231.  
  232.   j=wgetworldblock(wobject[YOU].x+16,wobject[YOU].y+extra,gamemap);
  233.   k=wgetworldblock(wobject[YOU].x+16,wobject[YOU].y+15,gamemap);
  234.   l=wgetworldblock(wobject[YOU].x+16,wobject[YOU].y+charheight-extra-1,gamemap);
  235.    if ((tiletype[j]==SOLID) || (tiletype[k]==SOLID) ||(tiletype[l]==SOLID))
  236.     {
  237.     j=wgetworldblock(wobject[YOU].x+15,wobject[YOU].y+extra,gamemap);
  238.     k=wgetworldblock(wobject[YOU].x+15,wobject[YOU].y+15,gamemap);
  239.     l=wgetworldblock(wobject[YOU].x+15,wobject[YOU].y+charheight-extra-1,gamemap);
  240.     while ((tiletype[j]==SOLID) || (tiletype[k]==SOLID)
  241.          ||(tiletype[l]==SOLID))
  242.     {
  243.        wobject[YOU].x--;
  244.   j=wgetworldblock(wobject[YOU].x+15,wobject[YOU].y+extra,gamemap);
  245.   k=wgetworldblock(wobject[YOU].x+15,wobject[YOU].y+15,gamemap);
  246.   l=wgetworldblock(wobject[YOU].x+15,wobject[YOU].y+charheight-extra-1,gamemap);
  247.  
  248.    }
  249.    }
  250. }
  251.  
  252. void checkleft(void)
  253. {
  254. int j,k,l;
  255.  
  256.   j=wgetworldblock(wobject[YOU].x-1,wobject[YOU].y+extra,gamemap);
  257.   k=wgetworldblock(wobject[YOU].x-1,wobject[YOU].y+15,gamemap);
  258.   l=wgetworldblock(wobject[YOU].x-1,wobject[YOU].y+charheight-extra-1,gamemap);
  259.    if ((tiletype[j]==SOLID) || (tiletype[k]==SOLID) ||(tiletype[l]==SOLID))
  260.     {
  261.   j=wgetworldblock(wobject[YOU].x,wobject[YOU].y+extra,gamemap);
  262.   k=wgetworldblock(wobject[YOU].x,wobject[YOU].y+15,gamemap);
  263.   l=wgetworldblock(wobject[YOU].x-1,wobject[YOU].y+charheight-extra-1,gamemap);
  264.    while ((tiletype[j]==SOLID) || (tiletype[k]==SOLID)
  265.      ||(tiletype[l]==SOLID))
  266.  {
  267.    wobject[YOU].x++;
  268.   j=wgetworldblock(wobject[YOU].x,wobject[YOU].y+extra,gamemap);
  269.   k=wgetworldblock(wobject[YOU].x,wobject[YOU].y+15,gamemap);
  270.   l=wgetworldblock(wobject[YOU].x,wobject[YOU].y+charheight-extra-1,gamemap);
  271.    }
  272.    }
  273. }
  274.  
  275. void checkfeet(void)
  276. {
  277. int j,k;
  278.  
  279.   j=wgetworldblock(wobject[YOU].x,wobject[YOU].y+charheight,gamemap);
  280.   k=wgetworldblock(wobject[YOU].x+15,wobject[YOU].y+charheight,gamemap);
  281.   if ((tiletype[j]==HOLLOW) & (tiletype[k]==HOLLOW))
  282.     jumping=1;
  283.     else {
  284.    jumping=0;
  285.    gravity=0;
  286.    j=wgetworldblock(wobject[YOU].x,wobject[YOU].y+charheight-1,gamemap);
  287.    k=wgetworldblock(wobject[YOU].x+15,wobject[YOU].y+charheight-1,gamemap);
  288.    while ((tiletype[j]==SOLID) || (tiletype[k]==SOLID)) {
  289.    wobject[YOU].y--;
  290.    j=wgetworldblock(wobject[YOU].x,wobject[YOU].y+charheight-1,gamemap);
  291.    k=wgetworldblock(wobject[YOU].x+15,wobject[YOU].y+charheight-1,gamemap);
  292.    }
  293.    }
  294. }
  295.  
  296. void checkhead(void)
  297. {
  298. int j,k;
  299.  
  300.   j=wgetworldblock(wobject[YOU].x,wobject[YOU].y-1,gamemap);
  301.   k=wgetworldblock(wobject[YOU].x+15,wobject[YOU].y-1,gamemap);
  302.   if ((tiletype[j]==SOLID) || (tiletype[k]==SOLID))
  303.     {
  304.    jumping=0;
  305.    gravity=0;
  306.    j=wgetworldblock(wobject[YOU].x,wobject[YOU].y,gamemap);
  307.    k=wgetworldblock(wobject[YOU].x+15,wobject[YOU].y,gamemap);
  308.    while ((tiletype[j]==SOLID) || (tiletype[k]==SOLID))
  309.     {
  310.    wobject[YOU].y++;
  311.    j=wgetworldblock(wobject[YOU].x,wobject[YOU].y,gamemap);
  312.    k=wgetworldblock(wobject[YOU].x+15,wobject[YOU].y,gamemap);
  313.    }
  314.    }
  315. }
  316.  
  317.  
  318.